﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SetWindowOpacity
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            List<ProcessModel> pmlist = new List<ProcessModel>();
            Process[] processes = Process.GetProcesses();
            foreach (var p in processes)
            {
                if (String.IsNullOrWhiteSpace(p.MainWindowTitle))
                    continue;
                pmlist.Add(new ProcessModel()
                {
                    Ptr = p.MainWindowHandle,
                    Title = p.MainWindowTitle
                });
            }
            this.cbProcess.ItemsSource = pmlist;
        }

        // hWnd是句柄，factor是透明度0~255
        bool MakeWindowTransparent(IntPtr hWnd, byte factor)
        {
            int LWA_ALPHA = 2;
            int GWL_EXSTYLE = -20;
            int WS_EX_LAYERED = 524288;
            int dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
            dwExStyle |= WS_EX_LAYERED;
            SetWindowLong(hWnd, GWL_EXSTYLE, (uint)dwExStyle);
            return SetLayeredWindowAttributes(hWnd, 0, factor, (uint)LWA_ALPHA);
        }

        private void slOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (cbProcess == null || cbProcess.SelectedItem == null)
            {
                return;
            }
            try
            {
                MakeWindowTransparent((cbProcess.SelectedItem as ProcessModel).Ptr, (byte)this.slOpacity.Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置失败:" + ex.Message);
            }
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        private void Close(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Min(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
    public class ProcessModel
    {
        public string Title { get; set; }
        public IntPtr Ptr { get; set; }
    }
}
